package com.ydsungan.web;
import com.ydsungan.bean.Clockin;
import com.ydsungan.bean.Signin;
import com.ydsungan.bean.Student;
import com.ydsungan.service.ClockinService;
import com.ydsungan.service.SigninService;
import com.ydsungan.service.StudentService;
import com.ydsungan.service.TeacherService;
import com.ydsungan.serviceimpl.ClockinServiceImpl;
import com.ydsungan.serviceimpl.SigninServiceImpl;
import com.ydsungan.serviceimpl.StudentServiceImpl;
import com.ydsungan.serviceimpl.TeacherServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SigninServlet extends HttpServlet {

    private ClockinService clockinService = null;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void init() throws ServletException {
        try {
            clockinService = new ClockinServiceImpl();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String address = request.getParameter("address");
        String temperature = request.getParameter("temperature");
        String remark = request.getParameter("remark");
        Integer contact = Integer.parseInt(request.getParameter("contact"));

        Signin signin = (Signin) this.getServletContext().getAttribute("today_signin_content");
        Student student = (Student) this.getServletContext().getAttribute("loginstudent");

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = df.format(new Date());

        Clockin clockin = new Clockin();
        clockin.setStudent(student.getId());
        clockin.setSignin(signin.getId());
        clockin.setAddress(address);
        clockin.setTemprature(temperature);
        clockin.setContact(contact);
        clockin.setRemark(remark);
        clockin.setDate(date);
        clockin.setDay(date.split(" ")[0]);

        PrintWriter writer = response.getWriter();

        try{
            clockinService.addClockin(clockin);
            this.getServletContext().setAttribute("student_has_clockin","1");
            clockin.setDate(clockin.getDate().split(" ")[1]);

            this.getServletContext().setAttribute("clockin_content", clockin);
            writer.write("{\"code\":\"1\",\"msg\":\"ok\"}");
        }catch (Exception e){
            e.printStackTrace();
            writer.write("{\"code\":\"0\",\"msg\":\"failure\"}");
        }



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
